Skip to content

🥳 New Feature 🥳 PlantUML visualization #1158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 42 commits into
base: main
Choose a base branch
from

Conversation

pdalfarr
Copy link

@pdalfarr pdalfarr commented Jul 5, 2024

The goal of this PR is to add the ability to dump a Spring stateMachine to PlanUML State Diagram format and to .png format 🥳

Features:

  1. Diagram Visualization 👁️
    Allow you to 'visualize' your Spring State Machine as .puml or .png !

  2. Current transition Highlight 🎯
    If a state machine is dumped at org.springframework.statemachine.StateContext.Stage.TRANSITION_END phase, a red arrow is used between the 'source' state and the 'target' state.
    see

    public String getArrowColor(boolean isCurrentTransaction) {
    return isCurrentTransaction ? "[#FF0000]" : "";
    }

  3. Actions and Guards description 🪪
    Support BeanAware for Actions and Guards, so bean names are used in diagram
    -> much better than having labels like Action@0x1f4f5d1d

  4. SpEL Expression support 🟰
    Support for SpelExpressionAction and SpelExpressionGuard : SpEL expression is used as label.

  5. Allow to 'tweak' the Diagram

  • Modifying PlantUML diagram settings 🎚️
  • Forcing Transition directions ➡️
  • Hiding some transitions 🙈
  • Adding a text note on State 📝

Unit tests:

  1. Instantiate a Spring State Machine by loading (already existing) .uml file from test folder
  2. Creating a PlantUML text representation of this state machine
  3. Comparing it to to expected result 'see (new) .puml files in test folder)

What does it looks like?

Here are some results (I dumped .puml files to .png):

action-with-transition-choice.uml -> action-with-transition-choice.puml :

image

multijoin-forkjoin.uml -> multijoin-forkjoin.puml :

image

Note:
Two *.uml files from test folder leads to inaccurate diagram.
These 2 errors, in simple-connectionpointref.puml and simple-root-regions.puml, are likely to be related to some limitations in spring statemachine UML parser.

Credit: The idea of creating this feature came to me while reading https://github.com/DeveloperUtils/spring-state-machine-chart-dumper

@pivotal-cla
Copy link

@pdalfarr Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pdalfarr pdalfarr changed the title PlantUML visualization New Feature - PlantUML visualization Jul 5, 2024
@libinbin880521
Copy link

libinbin880521 commented Jul 5, 2024 via email

@pivotal-cla
Copy link

@pdalfarr Thank you for signing the Contributor License Agreement!

Comment on lines +63 to +64
// It seems Spring statemachine does not support org.eclipse.uml2.uml.ConnectionPointReference !
// Arguments.of("org/springframework/statemachine/uml/simple-connectionpointref", null),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limitation in Spring state machine ?

Comment on lines +89 to +90
// It seems Spring statemachine UML parser creates duplicated transitions!
// Arguments.of("org/springframework/statemachine/uml/simple-root-regions", null),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in Spring state machine ?

Comment on lines 73 to 87
new PlantUmlWriterParameters<String>()
.hiddenTransition("S1", RIGHT, "S2")
),
Arguments.of("org/springframework/statemachine/uml/simple-guards", null),
Arguments.of("org/springframework/statemachine/uml/simple-history-deep", null),
Arguments.of("org/springframework/statemachine/uml/simple-history-default", null),
Arguments.of("org/springframework/statemachine/uml/simple-history-shallow", null),
Arguments.of("org/springframework/statemachine/uml/simple-junction", null),
Arguments.of("org/springframework/statemachine/uml/simple-localtransition",
// add some parameters to make diagram more readable
new PlantUmlWriterParameters<String>()
.arrowDirection("S22", UP, "S2")
.arrowDirection("S21", UP, "S2")
.arrowDirection("S22", UP, "S2")
.arrowDirection("S21", UP, "S2")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PlantUmlWriterParameters allow to tweak diagram by 'forcing' direction of some arrows / transitions

@pdalfarr
Copy link
Author

Hi @bclozel
I would like some Spring members to tell me if this PR can be useful for the Spring State Machine project or not.
It turns out that you are second in the list of contributors here, hence this message for you 😄
Can I kindly ask you to have a look at this.
I would be happy to elaborate more on why I think this could be useful to the project.
Kind Regards,
Pascal

@bclozel
Copy link
Member

bclozel commented Jul 12, 2024

Hi there!
This is probably a UI bug in GitHub as I don't remember contributing to statemachine and I'm not listed here https://github.com/spring-projects/spring-statemachine/graphs/contributors?from=2015-02-01&to=2024-07-12&type=c

Comment on lines 41 to 53
/**
* Implement a 'name strategy' based on this sequence:
* <UL>
* <LI>{@link Expression}</LI>
* <LI>{@link BeanNameAware}</LI>
* <LI>Lambda's unique "arg$1" parameter</LI>
* </UL>
* If all these strategy are failing, fallback to "class name" of the object
*
* @param object object to get "name" from
* @return name of the object
*/
public static String getName(Object object) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class should probably be compared to

private static <S, E> String actionToName(Function<StateContext<S, E>, Mono<Void>> action) {
return ObjectUtils.getDisplayString(action);
}
private static <S, E> String actionFunctionToName(Function<StateContext<S, E>, Mono<Void>> action) {
return ObjectUtils.getDisplayString(action);
}

which seems to be the current mean to get "names" of Spring State Machine actions, guard, etc..

@pdalfarr
Copy link
Author

@bclozel
Thanks for your reply.
Yes, this is likely a bug: your avatar is visible in second position on main page of the project and you did not contribute indeed.

I see you are a committer on some Spring projects, so I take the opportunity to ask you this: how I can do to reach out to Spring State Machine "owner(s)" ?
It looks like many PR are waiting here (not only mine) and I do not see much activity in Spring State Machine GitHub project.
Should we (user of Spring State Mchine) worry about this?
Is Spring State Machine still properly maintained?

( ? Maybe someone at Pivotal / VMWare could help regarding my questions ? )

@bclozel
Copy link
Member

bclozel commented Jul 12, 2024

I think @jvalkeal knows.

@pdalfarr
Copy link
Author

Hi @jvalkeal

Who can I reach out to, to discuss this PR?

Thanks

@pdalfarr
Copy link
Author

@fmbenhassine , we can discuss this whenever you want ;-)

@pdalfarr
Copy link
Author

pdalfarr commented Mar 11, 2025

Hi @fmbenhassine !
Thanks for your kind message :-)

I just made a change.
Is this OK with net.sourceforge.plantuml:plantuml-asl:1.2025.2?
Cfr f8507fb

( see https://sakirtemel.github.io/plantuml-alternative-docs/docs/main/faq.html#_i_dont_like_gpl )

@pdalfarr pdalfarr force-pushed the main branch 2 times, most recently from 4dd42f9 to b58a330 Compare March 12, 2025 07:20
Pascal Dal Farra and others added 20 commits March 12, 2025 08:45
…to illustrate the issue

Signed-off-by: Pascal Dal Farra <[email protected]>
Signed-off-by: Pascal Dal Farra <[email protected]>
Signed-off-by: Pascal Dal Farra <[email protected]>
Signed-off-by: Pascal Dal Farra <[email protected]>
Signed-off-by: Pascal Dal Farra <[email protected]>
… tests resources accordingly

Signed-off-by: Pascal Dal Farra <[email protected]>
Signed-off-by: Pascal Dal Farra <[email protected]>
Signed-off-by: Pascal Dal Farra <[email protected]>
… make 'Connection' cComparable

Signed-off-by: Pascal Dal Farra <[email protected]>
Signed-off-by: Pascal Dal Farra <[email protected]>
@pdalfarr
Copy link
Author

pdalfarr commented Mar 12, 2025

@fmbenhassine I am trying to fix "DCO Action required after 1s — DCO" regarding Signed-off-by: Author Name [email protected], but it looks like the changes I made are not taken into account.

On my side, I now see the required Signed-off on my commits, but the DCO issue is still listed here :/
I am afraid I wasn't able to fix this, and I must confess I don't know how to do

cfr
image

@fmbenhassine
Copy link
Contributor

Thank you for your feedback! The DCO issue is due to the fact that there are some commits among the 41 commits in that PR that are not signed off. You will need to squash all commits anyway.

Is this OK with net.sourceforge.plantuml:plantuml-asl:1.2025.2?

Yes, we can use that as long as the source code is available. Where is the source code of that module? We need the source code, not only a binary dependency. I also took some time to review the PR, and we need some updates:

  • Please remove Lombok usage and replace it with Java records (use regular classes if you need mutability)
  • The feature is about generating PlantUML diagrams and should focus on that. The code that generates images should be separate from that feature (one may ask why png and not jpeg?). This can be a utility class separate from the one that is related to the main feature. We can even have a separate PR that adds the ability to generate images from PlantUML diagrams as well as the existing Papyrus UML diagrams.
  • Please add javadocs to public APIs (at least the main methods of the feature that users will use the most).

Please let me know if I can help on any of these.

Apart from that, I just wanted to mention that we have no plan to ship a feature release for v4 for the moment (only bug fixes are planned for now). So this PR will target v5 at a minimum.

@fmbenhassine fmbenhassine added this to the 5.0.x milestone Mar 13, 2025
@pdalfarr
Copy link
Author

Thanks for your feedback.

  1. Source jar for plantuml-asl-1.2025.2.jar is available here :
    https://repo1.maven.org/maven2/net/sourceforge/plantuml/plantuml-asl/1.2025.2/
    https://repo1.maven.org/maven2/net/sourceforge/plantuml/plantuml-asl/1.2025.2/plantuml-asl-1.2025.2-sources.jar
  2. Okay, I'll remove Lombok.
  3. I'll (re)move png generation feature and probably create a dedicated PR for it. This makes sense.
  4. Javadoc addition on public API: Roger that!

Thanks for the clarification on which version of SSM will likely ship this pull request 👍

Signed-off-by: Pascal Dal Farra <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/uml Belongs to uml feature status/need-feedback Calling participant to provide feedback type/feature Is a feature request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants